
const db = wx.cloud.database()
const collectionName = 'pointOrder'

export default {
  async add(data) {
    data.createDate = new Date()
    return await db.collection(collectionName).add({
      data: data
    })
  },
  async get(id) {
    return await db.collection(collectionName).doc(id).get()
  },
  async update(data){
    let _id = data._id
    delete data._id
    var res = await db.collection(collectionName).doc(_id).update({
      data: data
    })
    return res
  },
  async getMyOrder({
    openid,
    pageNum,
    pageSize
  }) {
    var skip = (pageNum - 1) * pageSize
    var matchObj = {
      openid
    }

    let res = await db.collection(collectionName).aggregate()
      .match(matchObj)
      .sort({
        createDate: -1
      })
      .skip(skip)

      .limit(pageSize)
      .end()

    return res.list
  },
  async getSendGoodsList({
    pageNum,
    pageSize
  }) {
    var skip = (pageNum - 1) * pageSize
    let res = await db.collection(collectionName).aggregate()
      .match({
        status: 0
      })
      .skip(skip)
      .limit(pageSize)
      .end()

    return res.list
  }
}